package cn.school.web;

import cn.school.entity.Page;
import cn.school.entity.Student;
import cn.school.service.StudentService;
import cn.school.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = StringUtil.toIntGt0(req.getParameter("pageNo"), 1);
        int pageSize = 10;
        String sname = StringUtil.trimNull(req.getParameter("sname"));
        String qq = StringUtil.trimNull(req.getParameter("qq"));
        String telephone = StringUtil.trimNull(req.getParameter("telephone"));
        Date startDate = StringUtil.toDate(req.getParameter("startDate"));
        Date end= StringUtil.toDate(req.getParameter("endDate"));
        Page<Student> page = StudentService.INST.search(pageNo, pageSize, sname, qq, telephone,startDate,end);
        req.setAttribute("stus",page);
        req.setAttribute("sname",sname);
        req.setAttribute("qq",qq);
        req.setAttribute("startDate",startDate);
        req.setAttribute("endDate",end);
        req.setAttribute("telephone",telephone);
        req.getRequestDispatcher("/WEB-INF/jsp/student.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
